/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.*;

/**
 *
 * @author shinji
 */
@Stateless
public class Member1Facade extends AbstractFacade<Member1> {

    @PersistenceContext(unitName = "Matrimony-ejbPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public Member1Facade() {
        super(Member1.class);
    }

    public boolean isUsername(String Name) {
        try {
            Query q = this.em.createQuery("select m from Member1 m where m.username = :username");
            q.setParameter("username", Name);

            return q.getResultList().size() == 1 ? true : false;
        } catch (Exception e) {
            return false;
        }
    }

    public boolean isUsername(Member1 m) {
        try {
            Query q = this.em.createQuery("select m from Member1 m where m.username = :username and m.password = :password");
            q.setParameter("username", m.getUsername());
            q.setParameter("password", m.getPassword());

            return q.getResultList().size() == 1 ? true : false;
        } catch (Exception e) {
            return false;
        }
    }

    public Object get_Member_Username(String username) {
        try {
            Query q = this.em.createQuery("select m from Member1 m where m.username = :username");
            q.setParameter("username", username);

            return q.getSingleResult();
        } catch (Exception e) {
            return null;
        }
    }

    public void edit_Liking(List<Likings> ll, int member_id) {
        try {
            Query q = this.em.createNativeQuery("delete from Member_Likings where ID_Member=" + member_id);
            q.executeUpdate();

            for (Likings l : ll) {
                q = this.em.createNativeQuery("insert into Member_Likings values(" + member_id + "," + l.getId() + ")");
                q.executeUpdate();
            }
            this.em.flush();
        } catch (Exception e) {
            System.out.println("error");
            System.out.println(e.getMessage());
        }   
    }
    
    public List<Member1> GetFriendListCombined(int member_id){
        Query q = this.em.createQuery("SELECT f.iDMember from Friend f where f.iDFriend.id = :member_id and f.request = TRUE");
        q.setParameter("member_id", member_id);
        List<Member1> LM = q.getResultList();
        q = this.em.createQuery("SELECT f.iDFriend from Friend f where f.iDMember.id = :member_id and f.request = TRUE");
        q.setParameter("member_id", member_id);
        LM.addAll(q.getResultList());
        
        return LM;
    }
    
    public boolean isFriend(Member1 member1,Member1 member2){
        try {
            Query q = this.em.createQuery("select f from Friend f where f.iDFriend.id = :member1 and f.iDMember.id = :member2 and f.request = TRUE");
            q.setParameter("member1", member1.getId());
            q.setParameter("member2", member2.getId());                 
            List<Friend> LF = q.getResultList();
            q = this.em.createQuery("select f from Friend f where f.iDMember.id = :member1 and f.iDFriend.id = :member2 and f.request = TRUE");
            q.setParameter("member1", member1.getId());
            q.setParameter("member2", member2.getId());
            LF.addAll(q.getResultList());
                       
            return LF.size() > 0 ? true : false;
        } catch (Exception e) {
            return false;
        }
    }
    
    public boolean isRequestingFriend(Member1 member1,Member1 member2){
        try {
            Query q = this.em.createQuery("select f from Friend f where f.iDFriend.id = :member1 and f.iDMember.id = :member2 and f.request = FALSE");
            q.setParameter("member1", member1.getId());
            q.setParameter("member2", member2.getId());                 
            List<Friend> LF = q.getResultList();
                       
            return LF.size() > 0 ? true : false;
        } catch (Exception e) {
            return false;
        }
    }
    
    public boolean isRequestedFriend(Member1 member1,Member1 member2){
        try {
            Query q = this.em.createQuery("select f from Friend f where f.iDFriend.id = :member2 and f.iDMember.id = :member1 and f.request = FALSE");
            q.setParameter("member1", member1.getId());
            q.setParameter("member2", member2.getId());                 
            List<Friend> LF = q.getResultList();
                       
            return LF.size() > 0 ? true : false;
        } catch (Exception e) {
            return false;
        }
    }
    
    public List<Member1> GetRequestedFriendList(Member1 member1){
            Query q = this.em.createQuery("select f.iDFriend from Friend f where f.iDMember.id = :member1 and f.request = FALSE");
            q.setParameter("member1", member1.getId());                
            
            return q.getResultList();
    }
    
    
    public void flush(){
        this.em.flush();
    }
    
    public void clear(){
        this.em.clear();
    }

    public List<Member1> get_AllMember_Gender(String gender, Member1 member1) {
        try {
            if (gender.equals("mem")) {
                Query q = this.em.createQuery("select m from Member1 m where m.gender = :gender and m.id = :id");
                q.setParameter("gender", true);
                q.setParameter("id", member1.getId());
                
                return q.getResultList();
            }else{
                Query q = this.em.createQuery("select m from Member1 m where m.gender = :gender and m.id = :id");
                q.setParameter("gender", false);
                q.setParameter("id", member1.getId());
                
                return q.getResultList();
            }
        } catch (Exception e) {
            return null;
        }
    }
}
